//
//  OrdetailViewController.m
//  xingfugangwan
//
//  Created by rainday on 15/1/5.
//  Copyright (c) 2015年 rainday. All rights reserved.
//

#import "OrdetailViewController.h"
#import "OrdetailViewModel.h"
#import <ReactiveCocoa.h>
#import <RACEXTScope.h>
#import "QianyueTableViewController.h"

@interface OrdetailViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic ,strong) OrdetailViewModel *viewModel;
@property (nonatomic ,weak) IBOutlet UITableView *tableView;
@property (nonatomic ,weak) IBOutlet UIButton *editButton;
@property (nonatomic ,weak) IBOutlet NSLayoutConstraint *constraint;


@end

static NSString *reuseIdentify = @"Cell";

@implementation OrdetailViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil withOrderId:(id)orderId
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        
        _viewModel = [[OrdetailViewModel alloc]initWithOrderId:orderId];
        RACSignal *downedDataSignal = [_viewModel fetchOrderDataWithOrderId:orderId];
        
        @weakify(self)
        
        [[[downedDataSignal filter:^BOOL(id value) {
            @strongify(self)
            
            BOOL contion = [value isEqualToString:@"success"]?YES:NO;
            if (contion) {
                [self.tableView reloadData];
            }
            return contion;
        }]
          deliverOn:[RACScheduler mainThreadScheduler]] subscribeNext:^(id x) {
            
        }];
        
        
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    _tableView.layer.cornerRadius = 5;
    _tableView.layer.masksToBounds = YES;
    _editButton.layer.cornerRadius = 5;
    _editButton.layer.masksToBounds = YES;
    @weakify(self);
    [RACObserve(_viewModel, state) subscribeNext:^(NSString *state) {
        if ([state isEqualToString:@"已预约"]) {
            @strongify(self);
            [_editButton setTitle:@"去签约" forState:UIControlStateNormal];
            
            [[_editButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
                QianyueTableViewController *qianyue = [[QianyueTableViewController alloc] initWithNibName:NSStringFromClass([QianyueTableViewController class]) bundle:nil];
                qianyue.orderId = self.navigationItem.title;
                
                [self.navigationController pushViewController:qianyue animated:YES];
            }];
            
            
        }
        if ([state isEqualToString:@"已签约"]) {
            [_editButton setTitle:@"取消订单" forState:UIControlStateNormal];
            
            [[_editButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
               
                @strongify(self);
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您无权限进行该操作" delegate:self cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
                [alertView show];
                
                
            }];
        }
        if ([state isEqualToString:@"已完成"]) {
            _constraint.constant = 20;
            
        }
    }];
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/


- (void)didClickButton:(id)sender
{
    [_delegate dismissOrdetailViewController:self];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentify];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseIdentify];
    }
    
    cell.textLabel.text = [_viewModel titleForRowInIndexPath:indexPath];
    cell.detailTextLabel.text = [_viewModel detailForRowInIndexPath:indexPath];
    
    return cell;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_viewModel numberOfRowsInSection:section];
    

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [_viewModel numerOfSection];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 44;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 64)];
    view.backgroundColor = [UIColor lightGrayColor];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 300, 30)];
    label.font = [UIFont systemFontOfSize:22];
    label.textColor = [UIColor whiteColor];
    label.text = [_viewModel titleForHeaderOfSection:section];
    [view addSubview:label];
    return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 20;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor clearColor];
    return view;
}
@end
